﻿/* Autor: Achim Schnell */

using System.Linq;
using System.Reflection;
using AppFx.ComponentModel.Descriptive;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.ComponentModel.Descriptive
{
	[TestClass]
	public class Test_CompiledNestedPropertyDescriptor
	{
		[TestMethod]
		[Description("Testet, ob ein CompiledNestedPropertyDescriptor erstellt werden kann.")]
		public void Can_CreateCompiledNestedPropertyDescriptor()
		{
			CustomComplexType o = new CustomComplexType();
			PropertyInfo[] mainPis = o.GetType().GetProperties();
			PropertyInfo[] subPis = mainPis.Where(p => p.Name == "Sub").Single().PropertyType.GetProperties();

			CompiledNestedPropertyDescriptor descriptor = new CompiledNestedPropertyDescriptor(o.GetType(),
				subPis.Where(p => p.Name == "Name").Single(), new CompiledPropertyDescriptor(o.GetType(), mainPis.Where(p => p.Name == "Sub").Single()));
		}

		[TestMethod]
		[Description("Testet, ob ein CompiledNestedPropertyDescriptor einen Wert auslesen kann.")]
		public void Can_GetValue()
		{
			CustomComplexType o = new CustomComplexType();
			PropertyInfo[] mainPis = o.GetType().GetProperties();
			PropertyInfo[] subPis = mainPis.Where(p => p.Name == "Sub").Single().PropertyType.GetProperties();

			CompiledNestedPropertyDescriptor descriptor = new CompiledNestedPropertyDescriptor(o.GetType(),
				subPis.Where(p => p.Name == "Name").Single(), new CompiledPropertyDescriptor(o.GetType(), mainPis.Where(p => p.Name == "Sub").Single()));
			Assert.IsTrue(descriptor.GetValue(o).ToString() == o.Sub.Name);
		}

		[TestMethod]
		[Description("Testet, ob ein CompiledNestedPropertyDescriptor einen Wert setzen kann.")]
		public void Can_SetValue()
		{
			CustomComplexType o = new CustomComplexType();
			PropertyInfo[] mainPis = o.GetType().GetProperties();
			PropertyInfo[] subPis = mainPis.Where(p => p.Name == "Sub").Single().PropertyType.GetProperties();

			CompiledNestedPropertyDescriptor descriptor = new CompiledNestedPropertyDescriptor(o.GetType(),
				subPis.Where(p => p.Name == "Name").Single(), new CompiledPropertyDescriptor(o.GetType(), mainPis.Where(p => p.Name == "Sub").Single()));
			descriptor.SetValue(o, "Frau");
			Assert.IsTrue(descriptor.GetValue(o).ToString() == "Frau");
		}
	}

	public class CustomComplexType
	{
		public SubType Sub { get; set; }

		public CustomComplexType()
		{
			Sub = new SubType();
			Sub.Name = "Herr";
		}
	}

	public class SubType
	{
		public string Name { get; set; }
	}
}
